package heliotrope.config
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	import heliotrope.global.GlobalVar;
	import heliotrope.net.loader.ByteArrayTarget;
	import heliotrope.net.loader.CommonLoader;
	import heliotrope.net.loader.LoadMission;
	import heliotrope.net.loader.LoadType;
	import heliotrope.net.loader.QueueLoader;
	import heliotrope.net.loader.TextTarget;
	import heliotrope.net.loader.XMLTarget;
	import heliotrope.res.ResourceMission;
	import heliotrope.utils.mission.MissionManager;

	public class ConfigPool extends EventDispatcher
	{
		private static var __instance:ConfigPool;
		private var _source:String;
		private var _XMLConfigs:Object;
		private var _binaryConfigs:Object;
		private var _textConfigs:Object;
		private var _loadManager:QueueLoader;
		private var _missionManager:MissionManager;
		public function ConfigPool(pvt:PrivateClass)
		{
			_source = "testdata";
			_XMLConfigs = {};
			_binaryConfigs = {};
			_textConfigs = {};
			_loadManager = new QueueLoader(1);
			_missionManager = new MissionManager;
		}
		
		private function completeHandler(event:Event):void
		{
			dispatchEvent(event);
		}
		public function initConfigs():void
		{
			_missionManager.run();
		}
		private function loadBinaryConfig(url_:String, name_:String):void
		{
			var config:ByteArrayTarget = new ByteArrayTarget;
			_binaryConfigs[name_] = config;
			_loadManager.addMission(new LoadMission(_source+url_, config, LoadType.BINARY));
		}
		private function loadXMLConfig(url_:String, name_:String):void
		{
			var config:XMLTarget = new XMLTarget;
			_XMLConfigs[name_] = config;
			_loadManager.addMission(new LoadMission(_source+url_, config, LoadType.XML));
		}
		private function loadTextConfig(url_:String, name_:String):void
		{
			var config:TextTarget = new TextTarget;
			_textConfigs[name_] = config;
			_loadManager.addMission(new LoadMission(_source+url_, config, LoadType.TEXT));
		}
		public function getXMLConfig(name_:String):XML
		{
			var tmp:XMLTarget = _XMLConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.xml;
		}
		public function getBinaryData(name_:String):ByteArray
		{
			var tmp:ByteArrayTarget = _binaryConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.data;
		}
		public function getTextConfig(name_:String):String
		{
			var tmp:TextTarget = _textConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.text;
		}

		public static function init():void
		{
			if(ConfigPool.__instance)
			{
				throw new Error("You can't init the ConfigPool twice!")
			}
			ConfigPool.__instance = new ConfigPool(new PrivateClass);
		}
		
		public static function getInstance():ConfigPool
		{
			return ConfigPool.__instance;
		}

		public function get source():String{return _source;}
	}
}

class PrivateClass
{
	public function PrivateClass()
	{
	}
}